package com.lanxum.common.exception;

import com.lanxum.common.enums.CodeEnum;
import com.lanxum.common.utils.JsonResUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;


/**
 * @author: li.sun
 * @create: 2020-05-05 16:11
 * @description: 全局异常统一处理
 **/

public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public String defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        log.error("---BaseException Handler---Host {} invokes url {} ERROR: ",req.getRemoteHost(),req.getRequestURL(),e);
        return JsonResUtil.respJson(CodeEnum.SERVICE_ERROR);
    }

    @ExceptionHandler(value = AppException.class)
    public String jsonErrorHandle(HttpServletRequest req, AppException e) {
        log.warn("---BaseException Handler---Host {} invokes url {}  ",req.getRemoteHost(),req.getRequestURL());
        return JsonResUtil.respJson(e.getCode(),e.getMessage());
    }
}
